package com.aek.aekdroid;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;

public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	public static final String TAG = "PrefsActivity";
	private boolean firstTime;
	private AekDroidApplication aek;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs);
		aek = (AekDroidApplication) getApplication();
		firstTime = getIntent().getBooleanExtra("firstTime", false);
		for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
			initSummary(getPreferenceScreen().getPreference(i));
		}

		MyLog.d(TAG, "onCreate");
	}

	private void initSummary(Preference p) {
		if (p instanceof PreferenceCategory) {
			PreferenceCategory pCat = (PreferenceCategory) p;
			for (int i = 0; i < pCat.getPreferenceCount(); i++) {
				initSummary(pCat.getPreference(i));
			}
		} else if (p instanceof PreferenceScreen) {
			PreferenceScreen pScreen = (PreferenceScreen) p;
			for (int i = 0; i < pScreen.getPreferenceCount(); i++) {
				initSummary(pScreen.getPreference(i));
			}
		}

		else {
			updatePrefSummary(p);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		// Set up a listener whenever a key changes
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// if (firstTime) {
		// MyLog.d(TAG, "Updating for the first time");
		// Intent firstTimeUpdate = new Intent(this, MatchUpdaterService.class);
		// firstTimeUpdate.putExtra(MatchUpdaterService.MANUAL, true);
		// startService(firstTimeUpdate);
		// Toast.makeText(this, getText(R.string.toastUpdating),
		// Toast.LENGTH_LONG)
		// .show();
		// }
		// Unregister the listener whenever a key changes
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	private void updatePrefSummary(Preference p) {
		if (p instanceof ListPreference) {
			ListPreference listPref = (ListPreference) p;
			String v = listPref.getEntry() != null ? listPref.getEntry().toString() : "";
			p.setSummary(listPref.getDialogTitle() + ": " + v);
		}
		if (p instanceof EditTextPreference) {
			EditTextPreference editTextPref = (EditTextPreference) p;
			String v = editTextPref.getText() != null ? editTextPref.getText() : "";
			p.setSummary(editTextPref.getDialogMessage() + ": " + v);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		MyLog.d(TAG, "onDestroy");
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		Preference pref = findPreference(key);
		updatePrefSummary(pref);
		if (key.equals(getString(R.string.pref_updateFreqKey))) {
			ListPreference listPref = (ListPreference) pref;
			String v = listPref.getValue();
			if (v != null) {
				try {
					Long interval = Long.parseLong(v);
					Helper.setAlarm(TAG, getApplicationContext(), interval);
				} catch (NumberFormatException e) {

				}
			}
		} else if (key.equals(getString(R.string.pref_adminKey))) {
			MyLog.d(TAG, "Checking admin pass");
			EditTextPreference adminPref = (EditTextPreference) pref;
			String v = adminPref.getText();
			Helper.checkAdminPass(this, v);
		} else if (key.equals(getString(R.string.pref_calendarKey))) {
			CheckBoxPreference cal = (CheckBoxPreference) pref;
			Calendar calendar = new Calendar(this);
			if (aek.getCalendarBase() == null) {
				String base = calendar.getCalendarUriBase();
				aek.setCalendarBase(base);
			}
			String selCalendar = null;
			MyLog.d(TAG, "Calendar is checked");
			calendar.getCalendars();
			calendar.selectCalendar(aek, cal.isChecked());
		}
	}

}
